/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.submit;

import static javax.persistence.TemporalType.TIMESTAMP;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;


@Entity
@Table(name = "JOBHISTORY")
@NamedQueries({@NamedQuery(name="jobs", query = "select rs from RunStatus  rs where rs.username = :username order by rs.submittedTime desc"),@NamedQuery(name="numofjobs", query = "select count(rs.id) from RunStatus  rs where rs.username = :username"),
@NamedQuery(name="userjob", query = "select rs from RunStatus  rs where rs.username = :username and rs.jobid= :jobid"),
@NamedQuery(name="deletejob", query = "delete RunStatus rs where rs.id=:id"),
@NamedQuery(name="jobfromtime", query = "select count(rs.id) from RunStatus rs where  rs.username=:username  and rs.clusterid=-1 and rs.preSubmitTime >=:presubmittime")})
public class RunStatus implements Serializable {
    
	public static final long LOCAL_CLUSTER=-1;
	public static final long ANY_CLUSTER=0;
	/**
     *
     */
	
    private static final long serialVersionUID = 1L;
    @Id
    @Column(updatable=false)
	protected long id;
    @Column(name = "USERID", updatable = false)
    protected String username;
    @Column(name = "JOBID")
    protected String jobid;
    @Column(name = "PROGRAMNAME", updatable = false)
    protected String programName;
    @Column(name = "CMDLINE")
    protected String cmdline;
    
    @Column(updatable=false)
    @Temporal(TIMESTAMP)
    protected Date preSubmitTime;
    
    protected long clusterid;
    
    protected String note;
    protected String remoteJobId;
  
    public String getRemoteJobId()
	{
		return remoteJobId;
	}

	public void setRemoteJobId(String remoteJobId)
	{
		this.remoteJobId = remoteJobId;
	}

	protected int trials;

	@Column(name = "SUBMITTIME")
    @Temporal(TIMESTAMP)
    protected Date submittedTime;
    @Temporal(TIMESTAMP)
    @Column(name = "FINISHTIME")
    protected Date finishedTime;
    @Column(name = "STATUS")
    protected RunStatusCode code;
    @Column(name = "EXITVALUE")
    protected int exitValue;
    @Column(name = "WORKDIR", updatable = false)
    protected String workdir;
    
    @Basic
	protected String email;

    public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public boolean isRunning() {
        if ((code == RunStatusCode.RUNNING) ||
                (code == RunStatusCode.SUBMITTED) || (code==RunStatusCode.RETRIEVING)) {
            return true;
        } else {
            return false;
        }
    }

    public String getJobid() {
        return jobid;
    }

    public void setJobid(String jobid) {
        this.jobid = jobid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getProgramName() {
        return programName;
    }

    public void setProgramName(String programName) {
        this.programName = programName;
    }

    public String getCmdline() {
        return cmdline;
    }

    public void setCmdline(String cmdline) {
        this.cmdline = cmdline;
    }

    public Date getSubmittedTime() {
        return submittedTime;
    }

    public void setSubmittedTime(Date submittedTime) {
        this.submittedTime = submittedTime;
    }

    public Date getFinishedTime() {
        return finishedTime;
    }

    public void setFinishedTime(Date finishedTime) {
        this.finishedTime = finishedTime;
    }

    public RunStatusCode getCode() {
        return code;
    }

    public void setCode(RunStatusCode code) {
        this.code = code;
    }

    public int getExitValue() {
        return exitValue;
    }

    public void setExitValue(int exitValue) {
        this.exitValue = exitValue;
    }

    public String getWorkdir() {
        return workdir;
    }

    public void setWorkdir(String workdir) {
        this.workdir = workdir;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
    
    public String getWorkdirName()
    {
    	int index=workdir.lastIndexOf('/');
    	if (index==-1)
    	{
    		index=workdir.lastIndexOf('\\');
    	}
    	if (index==-1)
    		return workdir;
    	else
    		return workdir.substring(index+1);
    }
    
    public Date getPreSubmitTime()
	{
		return preSubmitTime;
	}

	public void setPreSubmitTime(Date preSubmitTime)
	{
		this.preSubmitTime = preSubmitTime;
	}

	public long getClusterid()
	{
		return clusterid;
	}

	public void setClusterid(long clusterid)
	{
		this.clusterid = clusterid;
	}
	 
	public String getNote()
	{
		return note;
	}

	public void setNote(String note)
	{
		this.note = note;
	}

	public int getTrials()
	{
		return trials;
	}

	public void setTrials(int trials)
	{
		this.trials = trials;
	}
	 public boolean isRemoteJob()
	    {
	    	return getClusterid() != RunStatus.LOCAL_CLUSTER;
	    }
}
